﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Web;

namespace DynDns
{
    public class accounts
    {
        private string roll;

        // key sollte USER sein
        public bool access(string key, string name, string pass) 
        {
            bool retBool = false;
            string value = "";

            // read from webconfig
            string filename = System.AppDomain.CurrentDomain.BaseDirectory + @"web.config";
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(filename);
            XmlNodeList nodeList = xmldoc.DocumentElement.ChildNodes;

            for (int i = 0; i < nodeList.Count; i++)
            {
                if (nodeList[i].Name.ToLower() == "appsettings")
                {
                    XmlNodeList node = nodeList[i].ChildNodes;
                    if (node.Count > 0)
                    {
                        foreach (XmlElement el in node)
                        {
                            if (el.Attributes["key"].InnerText == key)
                            {
                                value = el.Attributes["value"].InnerText;    //read the value
                                break;
                            }
                        }
                    }
                    break;
                }
            }

            // check user
            // BSP: value="admin|keks|admin, gast|eimer|guest, horst|klaus|user"
            string[] allUser = value.Split(',');
            foreach (string user in allUser) 
            {
                string[] aUser = user.Trim().Split('|');

                // 0 USERNAME, 1 PASS, 2 ROLLE
                if (aUser[0].Trim() == name && aUser[1].Trim() == pass) 
                {                    
                    roll = aUser[2];
                    retBool = true;
                    break;
                }                
            }
            return retBool;
        }

        public string getRoll() { return roll; }

    }
}